package com.kamistoat.meimeichat.meimeichatclient.socket;

import com.kamistoat.meimeichat.meimeichatclient.application.UIServiceConnector;
import com.kamistoat.meimeichat.meimeichatclient.socket.handlers.*;
import com.kamistoat.meimeichatserveraggrement.codec.ObjDecoder;
import com.kamistoat.meimeichatserveraggrement.codec.ObjEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

/**
 * Client端的init.
 * TCP协议时，应继承 SocketChannel 类
 */
public class NettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {

    private UIServiceConnector uiServiceConnector;

    public NettyClientChannelInitializer(UIServiceConnector uiServiceConnector) {
        this.uiServiceConnector = uiServiceConnector;
    }

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        //对象传输处理[解码]
        channel.pipeline().addLast(new ObjDecoder());
        // 在管道中添加我们自己的接收数据实现方法
        channel.pipeline().addLast(new AddFriendHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new LoginHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new MsgGroupHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new MsgHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new SearchFriendHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new TalkNoticeHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new ClientOnLineStatusNoticeHandler(this.uiServiceConnector));
        channel.pipeline().addLast(new TCP_EstablishP2PServer2ClientHandler(this.uiServiceConnector));
        //对象传输处理[编码]
        channel.pipeline().addLast(new ObjEncoder());
    }
}